﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

namespace mamnontuoitho7.AppCode
{
    public class ArticleType
    {
        private ConnectGetData _connectData = new ConnectGetData();
        private string _getAllArticleTypeSql = "select * from ArticleType where ArticleTypeID!=11";

        /// <summary>
        /// Get all information from Product type
        /// </summary>
        /// <returns></returns>
        public DataTable GetAllArticleType()
        {
            return _connectData.GetDataTable(_getAllArticleTypeSql);
        }

        /// <summary>
        /// Get all information from Product type
        /// </summary>
        /// <returns></returns>
        public DataTable GetAllArticleType2()
        {
            return _connectData.GetDataTable("select * from ArticleType");
        }

        /// <summary>
        /// Get all information from Product type
        /// </summary>
        /// <returns></returns>
        public DataTable GetAllArticleType3()
        {
            return _connectData.GetDataTable("select * from ArticleType where ArticleTypeID > 5");
        }

        public DataRow GetOneArticleTypeByID(String articleTypeByID)
        {
            return _connectData.GetDataTable("select * from ArticleType where ArticleTypeID=" + articleTypeByID).Rows[0];
        }

        /// <summary>
        /// check if the artycletype has the right superior or not
        /// </summary>
        /// <param name="username"></param>
        /// <param name="commentID"></param>
        /// <returns></returns>
        public bool checkArticleSuperior(String articleTypeID, String superior)
        {
            bool flag = false;

            try
            {
                string sql = "select * from ArticleType where ArticleTypeID = @ArticleTypeID and Superior =@Superior ";

                SqlCommand cmd = new SqlCommand(sql, _connectData.GetConnect());
                cmd.Parameters.AddWithValue("@ArticleTypeID", articleTypeID);
                cmd.Parameters.AddWithValue("@Superior", superior);
                DataTable dt = _connectData.GetDataTable(cmd);

                if (dt.Rows.Count > 0)
                {
                    flag = true;
                }
                cmd.Dispose();
            }
            catch (Exception ex)
            {
                System.Console.WriteLine("Error error error: " + ex.StackTrace);
                return flag;
            }
            return flag;
        }

        /// <summary>
        /// create article type
        /// </summary>
        /// <param name="productID"></param>
        /// <param name="memberID"></param>
        /// <param name="contentComment"></param>
        /// <returns> true if success</returns>
        public bool ArticleTypeCreate(String articleTypeName, String superior)
        {
            SqlConnection con = _connectData.GetConnect();
            con.Open();
            try
            {
                string sqlUp = "Insert into ArticleType (ArticleTypeName, Visible, Superior) values(@ArticleTypeName, @Visible, @Superior)";
                SqlCommand cmd = new SqlCommand(sqlUp, con);

                cmd.Parameters.AddWithValue("@ArticleTypeName", articleTypeName);
                cmd.Parameters.AddWithValue("@Visible", "true");
                cmd.Parameters.AddWithValue("@Superior", superior);


                cmd.ExecuteNonQuery();
                cmd.Dispose();
                con.Close();
            }
            catch (Exception ex)
            {

                System.Console.WriteLine("Co loiiii  : " + ex.StackTrace);
                return false;
            }
            return true;
        }

        /// <summary>
        /// update article type
        /// </summary>
        /// <param name="productID"></param>
        /// <param name="memberID"></param>
        /// <param name="contentComment"></param>
        /// <returns> true if success</returns>
        public bool ArticleTypeUpdate(String articleTypeID, String superior, String newArticleTypeName)
        {
            SqlConnection con = _connectData.GetConnect();
            con.Open();
            string sqlUp = "";
            


            try
            {
                if (newArticleTypeName == "")
                {
                    sqlUp = "update ArticleType set Superior = @Superior where ArticleTypeID=@ArticleTypeID";
                    SqlCommand cmd = new SqlCommand(sqlUp, con);

                    cmd.Parameters.AddWithValue("@ArticleTypeID", articleTypeID);
                    cmd.Parameters.AddWithValue("@Superior", superior);
                    cmd.ExecuteNonQuery();
                    cmd.Dispose();
                }
                else
                {
                    sqlUp = "update ArticleType set Superior = @Superior, ArticleTypeName = @ArticleTypeName where ArticleTypeID=@ArticleTypeID";
                    SqlCommand cmd = new SqlCommand(sqlUp, con);

                    cmd.Parameters.AddWithValue("@ArticleTypeID", articleTypeID);
                    cmd.Parameters.AddWithValue("@Superior", superior);
                    cmd.Parameters.AddWithValue("@ArticleTypeName", newArticleTypeName);
                    cmd.ExecuteNonQuery();
                    cmd.Dispose();
                }
                
                con.Close();
            }
            catch (Exception ex)
            {

                System.Console.WriteLine("Co loiiii  : " + ex.StackTrace);
                return false;
            }
            return true;
        }

        /// <summary>
        /// delete article type by ID 
        /// </summary>
        /// <param name="ProductID"></param>
        /// <returns></returns>
        public bool ArticleTypeDelete(String articleTypeID)
        {
            SqlConnection con = _connectData.GetConnect();
            con.Open();
            try
            {
                string sql = "delete from ArticleType  where ArticleTypeID = @ArticleTypeID";
                SqlCommand cmd = new SqlCommand(sql, con);
                cmd.Parameters.AddWithValue("@ArticleTypeID", articleTypeID);

                cmd.ExecuteNonQuery();
                cmd.Dispose();
                con.Close();
            }
            catch (Exception ex)
            {
                System.Console.WriteLine("Co loiiii  : " + ex.StackTrace);
                return false;
            }
            return true;
        }
    }
}